﻿using Abp.Authorization;
using Abp.Configuration.Startup;
using Abp.Localization;
using Abp.MultiTenancy;

namespace Akso.Authorization
{
    /// <summary>
    /// Application's authorization provider.
    /// Defines permissions for the application.
    /// See <see cref="AppPermissions"/> for all permission names.
    /// </summary>
    public class AppAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        public AppAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public AppAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //COMMON PERMISSIONS (FOR BOTH OF TENANTS AND HOST)

            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));

            #region Added by Hollis Yao
            //Basci Data Permissions
            
            var basicData = pages.CreateChildPermission(AppPermissions.Pages_BasicDataModule, L("Pages_BasicDataModule"));
            //教室设置
            var classrooms = basicData.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Classrooms, L("Pages_BasicDataModule_Classrooms"));
            classrooms.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Classrooms_Create, L("Pages_BasicDataModule_Classrooms_Create"));
            classrooms.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Classrooms_Edit, L("Pages_BasicDataModule_Classrooms_Edit"));
            classrooms.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Classrooms_Delete, L("Pages_BasicDataModule_Classrooms_Delete"));
            //基础代码设置
            //菜单名称
            var codes = basicData.CreateChildPermission(AppPermissions.Pages_BasicDataModule_CodeSettings, L("Pages_BasicDataModule_CodeSettings"));
            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_CodeGroups_Create, L("Pages_BasicDataModule_CodeGroups_Create"));
            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_CodeGroups_Edit, L("Pages_BasicDataModule_CodeGroups_Edit"));
            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_CodeGroups_ChildCode, L("Pages_BasicDataModule_CodeGroups_ChildCode"));

            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Codes_Create, L("Pages_BasicDataModule_Codes_Create"));
            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Codes_Edit, L("Pages_BasicDataModule_Codes_Edit"));
            codes.CreateChildPermission(AppPermissions.Pages_BasicDataModule_Codes_Delete, L("Pages_BasicDataModule_Codes_Delete"));
            #region 教务管理
            #region 教务管理
            var eduAdmin = pages.CreateChildPermission(AppPermissions.Pages_EduAdminModule, L("Pages_EduAdminModule"));
            var courses = eduAdmin.CreateChildPermission(AppPermissions.Pages_EduAdminModule_Courses, L("Pages_EduAdminModule_Courses"));
            courses.CreateChildPermission(AppPermissions.Pages_EduAdminModule_Courses_Create, L("Pages_EduAdminModule_Courses_Create"));
            courses.CreateChildPermission(AppPermissions.Pages_EduAdminModule_Courses_Edit, L("Pages_EduAdminModule_Courses_Edit"));
            courses.CreateChildPermission(AppPermissions.Pages_EduAdminModule_Courses_Delete, L("Pages_EduAdminModule_Courses_Delete"));
            #endregion

            #endregion
            #endregion

            var administration = pages.CreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));

            var roles = administration.CreateChildPermission(AppPermissions.Pages_Administration_Roles, L("Roles"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Create, L("CreatingNewRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Edit, L("EditingRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Delete, L("DeletingRole"));

            var users = administration.CreateChildPermission(AppPermissions.Pages_Administration_Users, L("Users"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Create, L("CreatingNewUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Edit, L("EditingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Delete, L("DeletingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_ChangePermissions, L("ChangingPermissions"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Impersonation, L("LoginForUsers"));

            var languages = administration.CreateChildPermission(AppPermissions.Pages_Administration_Languages, L("Languages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Create, L("CreatingNewLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Edit, L("EditingLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Delete, L("DeletingLanguages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_ChangeTexts, L("ChangingTexts"));

            administration.CreateChildPermission(AppPermissions.Pages_Administration_AuditLogs, L("AuditLogs"));

            var organizationUnits = administration.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits, L("OrganizationUnits"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree, L("ManagingOrganizationTree"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers, L("ManagingMembers"));

            //TENANT-SPECIFIC PERMISSIONS

            pages.CreateChildPermission(AppPermissions.Pages_Tenant_Dashboard, L("Dashboard"), multiTenancySides: MultiTenancySides.Tenant);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Tenant_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Tenant);

            //HOST-SPECIFIC PERMISSIONS

            var editions = pages.CreateChildPermission(AppPermissions.Pages_Editions, L("Editions"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Create, L("CreatingNewEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Edit, L("EditingEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Delete, L("DeletingEdition"), multiTenancySides: MultiTenancySides.Host);

            var tenants = pages.CreateChildPermission(AppPermissions.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Create, L("CreatingNewTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Edit, L("EditingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_ChangeFeatures, L("ChangingFeatures"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Delete, L("DeletingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Impersonation, L("LoginForTenants"), multiTenancySides: MultiTenancySides.Host);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Host);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Maintenance, L("Maintenance"), multiTenancySides: _isMultiTenancyEnabled ? MultiTenancySides.Host : MultiTenancySides.Tenant);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_HangfireDashboard, L("HangfireDashboard"), multiTenancySides: _isMultiTenancyEnabled ? MultiTenancySides.Host : MultiTenancySides.Tenant);
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AbpZeroTemplateConsts.LocalizationSourceName);
        }
    }
}
